package com.hhdys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhdys.dao.AccountMapper;
import com.hhdys.model.Account;
import com.hhdys.model.AccountExample;
import com.hhdys.shiro.DefaultRealm;

@Service
public class AccountServiceImpl implements AccountService {
	@Autowired
	private AccountMapper dao;
	@Autowired
	private DefaultRealm realm;

	@Override
	public Account selAccountByUsernameAndPwd(String username, String pwd) {
		AccountExample example = new AccountExample();
		AccountExample.Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		criteria.andPasswdEqualTo(DigestUtils.md5Hex(pwd));
		criteria.andInuseEqualTo(1);
		List<Account> list = dao.selectByExample(example);
		if (list.size() == 1) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * @return the dao
	 */
	public AccountMapper getDao() {
		return dao;
	}

	/**
	 * @param dao
	 *            the dao to set
	 */
	public void setDao(AccountMapper dao) {
		this.dao = dao;
	}

	@Override
	public int updatePwd(String passwd, String oldPasswd, int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("passwd", DigestUtils.md5Hex(passwd));
		map.put("id", userId);
		map.put("oldPwd", DigestUtils.md5Hex(oldPasswd));
		return dao.updatePwd(map);
	}

	@Override
	public List<Map<String, Object>> accoutsShow(String username, int curPage,
			int pageSize) {
		StringBuilder condition = new StringBuilder();
		;
		if (!"".equals(username)) {
			condition.append(" and a.username like '%" + username + "%'");
		}
		condition.append(" limit " + (curPage - 1) * pageSize + "," + pageSize);
		return dao.accoutsShow(condition.toString());
	}

	@Override
	public int accoutCount(String username) {
		AccountExample example = new AccountExample();
		AccountExample.Criteria criteria = example.createCriteria();
		if (!"".equals(username)) {
			criteria.andUsernameLike("%" + username + "%");
		}
		return dao.countByExample(example);
	}

	@Override
	public boolean addAccount(Account account) {
		return dao.insert(account) > 0;
	}

	@Override
	public boolean delAccount(String ids) {
		String[] Strs = ids.split(",");
		List<Integer> list = new ArrayList<Integer>();
		for (String str : Strs) {
			list.add(NumberUtils.toInt(str));
		}
		AccountExample example = new AccountExample();
		AccountExample.Criteria criteria = example.createCriteria();
		criteria.andIdIn(list);
		return dao.deleteByExample(example) > 0;
	}

	@Override
	public int getAccountsByUserName(String username) {
		if (username == null || "".equals(username)) {
			return 0;
		}
		AccountExample example = new AccountExample();
		AccountExample.Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		return dao.countByExample(example);
	}

	@Override
	public boolean changeAccountStat(int stat, int account) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("inuse", stat);
		map.put("accountId", account);
		return dao.changeAccountStat(map) > 0;
	}

	@Override
	public Account getAccountById(int id) {
		return dao.selectByPrimaryKey(id);
	}

	@Override
	public boolean editAccount(Account account) {
		// 清空权限缓存
		SimplePrincipalCollection principals = new SimplePrincipalCollection();
		principals.add(account.getUsername(), realm.getName());
		realm.clearCachedAuthorizationInfo(principals);
		return dao.updateByPrimaryKeySelective(account) > 0;
	}

	@Override
	public void updateLastLoginTime(Map<String, Object> map) {
		dao.updateLastLoginTime(map);
	}

	@Override
	public Account getAccountByUserName(String username) {
		AccountExample example = new AccountExample();
		AccountExample.Criteria criteria = example.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<Account> list = dao.selectByExample(example);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Account> getAccountsByRole(int role) {
		AccountExample example = new AccountExample();
		AccountExample.Criteria criteria = example.createCriteria();
		criteria.andRoleEqualTo(role);
		return dao.selectByExample(example);
	}

	/**
	 * @return the realm
	 */
	public DefaultRealm getRealm() {
		return realm;
	}

	/**
	 * @param realm
	 *            the realm to set
	 */
	public void setRealm(DefaultRealm realm) {
		this.realm = realm;
	}

	@Override
	public Set<Integer> selectPageIdsByAccountId(int accountId) {
		return dao.selectPageIdsByAccountId(accountId);
	}
}
